<?php

class imageHelper {
		
	/**
	* Upload image
	* @param $fieldName : Image name, alaway use : $_FILES['file_field_name']['name']
	* @param $imageName : New name of uploaded image 
	* @param $maxFileSize : Defaut 1000000 = 1 MB
	* @param $image_package_max_width, $image_package_max_height : Use to resize
	* @param $tempDir : Temporary Directory which image will be uploaded to before resize and copy to destination Dir
	* @param $desDir : destination Dir
	* @returns array Array of Image name, Image Size, Image Type
	*/
	function uploadImage($imgInfo, $tempDir,$desDir, $thumbDir, $max_upload_size=20000000, $max_width=800, $max_height=600) 
	{
		$imageType = array('image/jpeg','image/pjpeg','image/png','image/gif');
            
		$file_tmp = $imgInfo['tmp_name']; // $file_tmp is where file went on webserver
		$file_name = $imgInfo['name']; // $file_tmp_name is original file name
		$file_size = $imgInfo['size']; // $file_size is size in bytes
		$file_type = $imgInfo['type']; // $file_type is mime type e.g. image/gif
		$file_error = $imgInfo['error']; // $file_error is any error encountered
		
		// check for generic errors first		  
		if ($file_error > 0) 
		{
			echo "<script> alert('";
			switch ($file_error) 
			{
			  case 1:  echo _MESSAGE_UPLOAD_ERROR_PHP_UP_MAX_FILESIZE;  break;
			  case 2:  echo _MESSAGE_UPLOAD_ERROR_PHP_MAX_FILESIZE;  break;
			  case 3:  echo _MESSAGE_UPLOAD_ERROR_PARTIAL_UPLOAD;  break;
			  case 4:  echo _MESSAGE_UPLOAD_ERROR_NO_FILE;  break;
			}
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		// check custom max_upload_size passed into the function
		if ($max_upload_size < $file_size) 
		{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_MAX_FILESIZE;
			echo ' max_upload_size: '.$max_upload_size.' | file_size: '.$file_size;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		//check file type is valid
		$type_ok = 0;
		foreach ($imageType as $type) 
		{
			if ($file_type == $type) 
			{
				$type_ok = 1;
			}
		}
		
		if ($type_ok == 0) 
		{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_FILETYPE;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		// CHECK FOR SPECIAL CHARACTERS
		$special_chars = array('Ã¡','Ã¤','Ã ','Ã©','Ã«','Ã¨','Ã­','Ã¯','Ã¬','Ã³','Ã¶','Ã²','Ãº','Ã¼','Ã¹','Ã?','Ã','Ã','Ã','Ã','Ã','Ã?','Ã?','Ã','Ã','Ã','Ã','Ã','Ã','Ã','Ã±','Ã','?','Â¿','!','Â¡','(',')','[',']',',');
		foreach ($special_chars as $special_char) 
		{
			$file_name = str_replace($special_char, '', $file_name);
		}
		
		//rename image
		$file_name = imageHelper::renameImage($file_name);
		$path = $tempDir.$file_name;
		
		// is_uploaded_file and move_uploaded_file
		if (is_uploaded_file($file_tmp)) {
			
			if (!move_uploaded_file($file_tmp, $path)) 
			{
				echo "<script> alert('";
				echo _MESSAGE_UPLOAD_ERROR_MOVE;
				echo "'); window.history.go(-1); </script>\n";
				exit;
			}
		} else	{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_ATTACK.' '.$file_name;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		imageHelper::resize($tempDir.$file_name,$desDir.$file_name,$max_width,$max_height);
		if($thumbDir) imageHelper::resize($tempDir.$file_name,$thumbDir.$file_name,145,100);
		
		imageHelper::removeImage($tempDir.$file_name);
		//$array = array($file_name, $file_size, $file_type);
		
		return $file_name;
	}

	function renameImage ($imageName) {
		$date = str_replace("-",'',date('Y-m-d-H-m-s'));
        $imageName =  str_replace(" ",'',$imageName);
        $imageName =  str_replace(",",'',$imageName);
		return substr($imageName,0,strrpos($imageName,".")).'_'.$date.substr($imageName,strrpos($imageName,"."));
	}
	
	function removeImage ($namePath) {
		unlink ($namePath);
	}
	
	function resize($sourcefile, $destfile, $max_width=800, $max_height=600)  
	{
		// set the quality, 0 best quality | 100 worst quality
		//$img_quality = 100-$imgcomp;

		if(file_exists($sourcefile)) 
		{
			$image_size = getimagesize($sourcefile);
			$src_width = $image_size[0];
			$src_height = $image_size[1];
			$src_type = $image_size[2]; // 1 = GIF, 2 = JPG, 3 = PNG
			
			$new_width = $src_width;
			$new_height = $src_height;
			
			if ($new_width > $max_width) {
				$new_width = $max_width;
				$new_height = ($max_width/$new_width)*$new_height;
			} 
			if ($new_height > $max_height) {
				$new_height = $max_height;
				$new_width = ($max_height/$new_height)*$new_width;
			}
			
/*			
			if(($src_width/$max_width)>=($src_height/$max_height)) 
			{
				$new_width = $max_width;
				$new_height = ($max_width/$src_width)*$src_height;
			}
			//otherwise do as commented above but swap width for height
			else 
			{
				$new_height = $max_height;
				$new_width = ($new_height/$src_height)*$src_width;   
			}*/
			
			//create an image resource from the source file, this function will return $a, which
			//will become $img_src
			$img_src = imageHelper::img_create_from_file($sourcefile, $src_type);
			
			//create an image resource for the destination file, use the new width and height
			//caculated above, returns $a, which will become $img_dst
			$img_dst = imageHelper::img_create($new_width, $new_height, $src_type);
			
			imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $src_width, $src_height);

			imageHelper::img_output($img_dst, $destfile, 100, $src_type);
			
			imagedestroy($img_dst);
			//imageHelper::removeImage($sourcefile);
			
			$new_size_array = array();
			$new_size_array['width'] = $new_width;
			$new_size_array['height'] = $new_height;
		}
	}
	
	function img_create_from_file($sourcefile, $src_type) 
	{
		ini_set('memory_limit', '32M');
		switch ($src_type) 
		{
			case 1: // for gif
				$a = imagecreatefromgif($sourcefile);
				return $a;
			case 2: // for jpeg
				$a = imagecreatefromjpeg($sourcefile);
				return $a;
			case 3: // for png
				$a = imagecreatefrompng($sourcefile);
				return $a;
		}	
	}
	
	function img_create($new_width, $new_height, $src_type) 
	{
		switch ($src_type) 
		{
			case 1: // for gif
				$a = imagecreate($new_width, $new_height);
				return $a;
			case (2 || 3): // for jpeg and png
				$a = imagecreatetruecolor($new_width, $new_height);
				return $a;
		}		
	}
	
	function img_output($img_dst, $destfile, $img_quality=100, $src_type) 
	{
		switch ($src_type) 
		{
			case 1: // for gif
				imagegif($img_dst, $destfile);
				break;
			case 2: // for jpeg
				imagejpeg($img_dst, $destfile, $img_quality); 
				break;
			case 3: // for png
				$g_imgcomp /= 10;
				if($g_imgcomp > 9) $g_imgcomp = 9;
				imagepng($img_dst, $destfile, $img_quality);
				break;
		}		
	}
	
		function uploadAnimatedImage($imgInfo, $desDir, $max_upload_size=10000000) {
		
		//check error
		$file_tmp = $imgInfo['tmp_name']; // $file_tmp is where file went on webserver
		$file_name = $imgInfo['name']; // $file_tmp_name is original file name
		$file_size = $imgInfo['size']; // $file_size is size in bytes
		$file_type = $imgInfo['type']; // $file_type is mime type e.g. image/gif
		$file_error = $imgInfo['error']; // $file_error is any error encountered
		
		// check for generic errors first		  
		if ($file_error > 0) 
		{
			echo "<script> alert('";
			switch ($file_error) 
			{
			  case 1:  echo _MESSAGE_UPLOAD_ERROR_PHP_UP_MAX_FILESIZE;  break;
			  case 2:  echo _MESSAGE_UPLOAD_ERROR_PHP_MAX_FILESIZE;  break;
			  case 3:  echo _MESSAGE_UPLOAD_ERROR_PARTIAL_UPLOAD;  break;
			  case 4:  echo _MESSAGE_UPLOAD_ERROR_NO_FILE;  break;
			}
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		// check custom max_upload_size passed into the function
		if ($max_upload_size < $file_size) 
		{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_MAX_FILESIZE;
			echo ' max_upload_size: '.$max_upload_size.' | file_size: '.$file_size;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		//check file type is valid
		if ($file_type != 'image/gif')
		{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_FILETYPE;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		// CHECK FOR SPECIAL CHARACTERS
		$special_chars = array('Ã¡','Ã¤','Ã ','Ã©','Ã«','Ã¨','Ã­','Ã¯','Ã¬','Ã³','Ã¶','Ã²','Ãº','Ã¼','Ã¹','Ã?','Ã','Ã','Ã','Ã','Ã','Ã?','Ã?','Ã','Ã','Ã','Ã','Ã','Ã','Ã','Ã±','Ã','?','Â¿','!','Â¡','(',')','[',']',',');
		foreach ($special_chars as $special_char) 
		{
			$file_name = str_replace($special_char, '', $file_name);
		}
		
		//rename image
		$file_name = 'd_'.imageHelper::renameImage($file_name);
		$path = $desDir.$file_name;
		
		// is_uploaded_file and move_uploaded_file
		if (is_uploaded_file($file_tmp)) {
			
			if (!move_uploaded_file($file_tmp, $path)) 
			{
				echo "<script> alert('";
				echo _MESSAGE_UPLOAD_ERROR_MOVE;
				echo "'); window.history.go(-1); </script>\n";
				exit;
			}
		} else	{
			echo "<script> alert('";
			echo _MESSAGE_UPLOAD_ERROR_ATTACK.' '.$file_name;
			echo "'); window.history.go(-1); </script>\n";
			exit;
		}
		
		return $file_name;
	}
	
	
	
}

?>